<template>
	<view>
		<u-popup :show="screen_show" @close="close" @open="open" mode="top">
			<view>
				<view class="screen_box">
					<!-- <text>支付方式</text> -->
					<view class="screen_option">
						<text :class="on_index == ''?'on':''" @click="_tab(0)">全部</text>
						<text :class="on_index == 1?'on':''" @click="_tab(1)">收入</text>
						<text :class="on_index == 2?'on':''" @click="_tab(2)">支出</text>
					</view>
					<!-- <text>交易日期区间</text> -->
					<view class="screen_option">
						<u-datetime-picker style="flex: 0;" :show="start" v-model="start_value" mode="date"
							:min-date="minday" :max-date="maxday" @confirm="confirm($event,1)" closeOnClickOverlay
							@cancel="cancel(1)"></u-datetime-picker>
						<text class="datetime" @click="start = true">{{start_text}}</text>
						<view style="margin: 0 30rpx;">至</view>
						<u-datetime-picker style="flex: 0;" :show="end" v-model="end_value" mode="date"
							:min-date="minday" :max-date="maxday" @confirm="confirm($event,2)" closeOnClickOverlay
							@cancel="cancel(2)">
						</u-datetime-picker>
						<text class="datetime" @click="end = true">{{end_text}}</text>
					</view>
				</view>
				<view class="btn">
					<text @click="btn_confirm">确认</text>
				</view>
			</view>
		</u-popup>
		<u-sticky bg-color="#fff">
			<view class="balance">
				<view class="balance_top">
					<text>{{text_data}}</text>
					<text @click="screen_show = true">筛选</text>
				</view>
			</view>
		</u-sticky>
		<view class="account_box">
			<view class="account_item" v-if="display">
				<view class="account_list" v-for="(item,index) in data" :key="index">
					<view class="account_cont">
						<image :src="'/static/balanceType/balance1.png'" mode=""></image>
						<view class="" style="width: 100%;">
							<view class="increase">
								<text>{{item.mome}}</text>
								<text v-if="item.isChg == 1" class="green-txt">+{{item.money}}</text>
								<text v-else class="red-txt">-{{item.money}}</text>
							</view>
							<view v-if="item.product_name" class="msg" style="font-size: 24rpx; color: #c8c9cc; padding-bottom: 10rpx;">
								{{item.product_name}}
							</view>
							<u--text :text="item.create_time" color="#999999" align="left" size="24rpx"></u--text>
						</view>
					</view>
				</view>
			</view>
			<u-empty v-else mode="list" icon="/static/Empty/data_null.png" text="您还没有收益呢">
			</u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tab: [],
				current: 0,
				data: [],
				page: 1,
				start: false,
				start_text: '选择开始日期',
				start_value: Number(new Date()),
				start_time: '',
				end: false,
				end_text: '选择结束日期',
				end_value: Number(new Date()),
				end_time: '',
				screen_show: false,
				on_index: '',
				bottom: true,
				display: false,
				search_type: false,
				text_data: this.maxday,
				userinfo: {}
			}
		},
		//上拉加载
		onReachBottom() {
			if (this.bottom == true) {
				this.page += 1
				this._request()
			}
		},
		onLoad(e) {
			this.userinfo = uni.getStorageSync('userinfo');
			uni.pageScrollTo({
				scrollTop: this.scrollTop
			});
			let _date = new Date();
			_date.setTime(_date.getTime() - 24 * 60 * 60 * 1000);
			this.text_data = _date.getFullYear() + "-" + (_date.getMonth() + 1) + "-" + _date.getDate()
			this.start_time = this.text_data
			this.end_time = this.text_data
			this._request()
		},
		computed: {
			maxday() {
				let day = new Date();
				day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
				let days = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate()
				let max_day = new Date(days).getTime()
				return max_day
			},
			minday() {
				let day = new Date(); // 获取今天的日期
				day.setDate(day.getDate() - 180) // 今天的前N天的日期，N自定义
				let days = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate()
				let min_day = new Date(days).getTime()
				return min_day
			}
		},
		methods: {
			_tab(type) {
				if (type == 0) {
					this.on_index = ''
				} else {
					this.on_index = type
				}
			},
			btn_confirm() {
				uni.pageScrollTo({
					scrollTop: this.scrollTop
				});
				this.screen_show = false
				this.search_type = true
				this.page = 1
				this.data = []
				this.bottom = true
				if (this.start_time != '' && this.end_time != '') {
					this.text_data = this.start_time + ' 至 ' + this.end_time
				}
				this._request()
			},
			open() {
			},
			close() {
				this.screen_show = false
			},
			confirm(e, id) {
				const timeFormat = uni.$u.timeFormat
				switch (id) {
					case 1:
						//开始的时间
						this.start_text = timeFormat(e.value, 'yyyy-mm-dd')
						this.start_time = timeFormat(e.value, 'yyyy-mm-dd')
						this.start = false
						break
					case 2:
						//结束的时间
						this.end_text = timeFormat(e.value, 'yyyy-mm-dd')
						this.end_time = timeFormat(e.value, 'yyyy-mm-dd')
						this.end = false
						break
				}
			},
			cancel(id) {
				if (id == 1) {
					this.start = false
				} else if (id == 2) {
					this.end = false
				}
			},
			async _request() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Balance/details',
					data: {
						isChg: this.on_index,
						page: this.page,
						page_size: 10,
						start_time: this.start_time,
						date: this.end_time
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				if (res.code == 1) {
					if (this.search_type) {
						if (res.data !== undefined && res.data !== null && res.data.length >
							0) {
							this.display = true
							if (this.page == 1) {
								this.data = res.data
							} else {
								this.data = [...this.data, ...res.data]
							}
						} else {
							this.bottom = false
							this.display = false
							if (this.page > 1) {
								this.display = true
							}
						}
					} else {
						if (res.data !== undefined && res.data !== null && res.data.length >
							0) {
							this.display = true
							if (this.page == 1) {
								this.data = res.data
							} else {
								this.data = [...this.data, ...res.data]
							}
						} else {
							this.bottom = false
							this.display = false
							if (this.page > 1) {
								this.display = true
							}
						}
					}
				} else {
					this.bottom = false
					this.display = false
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f4f6;
	}
	.screen_box {
		padding: 30rpx;
		box-sizing: border-box;

		.screen_option {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			// justify-content: space-between;
			margin: 30rpx 0;

			text {
				background-color: #eee;
				border: #eee 1px solid;
				padding: 10rpx 20rpx;
				color: #333;
				border-radius: 15rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 0 5rpx 30rpx 5rpx;
			}

			text.on {
				background-color: #eee;
				border: #55aaff 1px solid;
				padding: 10rpx 20rpx;
				color: #55aaff;
				border-radius: 15rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.datetime {
				margin: 0;
			}
		}
	}

	.btn {
		display: flex;
		align-items: center;
		justify-content: center;

		text {
			width: 100%;
			padding: 30rpx 0;
			text-align: center;
			background-color: #55aaff;
			color: #fff;
		}
	}

	.balance {
		display: flex;
		flex-direction: column-reverse;
		padding: 30rpx;
		box-sizing: border-box;
		margin-bottom: 30rpx;

		.balance_top {
			display: flex;
			justify-content: space-between;
			// flex-direction: column;
			// align-items: center;
			width: 100%;

			text {
				font-size: 28rpx;
				color: #666666;
			}

			h2 {
				font-weight: 500;
				margin-top: 10rpx;
				margin-bottom: 30rpx;
				font-size: 42rpx;
			}
		}


		.expenses {
			display: flex;
			margin-bottom: 30rpx;
			box-sizing: border-box;
			align-items: center;
			justify-content: center;

			.dbs {
				// width: 100%;
			}

			.expenses_time {
				display: flex;
				justify-content: flex-start;
				align-items: center;

				.order {
					color: #fff;
					font-size: 28rpx;
					padding: 5rpx 20rpx;
					background-color: #1e80ff;
					border-radius: 15rpx;
					margin-left: 50rpx;
				}
			}

			text {
				font-size: 24rpx;
				color: #999999;
			}
		}
	}

	.account_box {
		padding-bottom: 20rpx;

		.account_item {
			padding: 0 20rpx;
		}

		.account_list {
			padding: 25rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			border-radius: 15rpx;
			margin-bottom: 20rpx;
			box-shadow: 7px 0px 30px -18px #888888;

			.account_cont {
				display: flex;
				align-items: center;
				width: 100%;

				image {
					width: 60rpx;
					height: 55rpx;
					margin-right: 20rpx;
				}

				text {
					color: #999;
					font-size: 24rpx;
					// margin-bottom: 20rpx;
				}
			}

			.increase {
				display: flex;
				justify-content: space-between;
				font-size: 24rpx;
				margin-bottom: 10rpx;
				width: 100%;

				text {
					color: #333;
					font-size: 28rpx;
				}

				.green-txt {
					color: #19be6b;
				}
				
				.red-txt {
					color: #fa3534;
				}
			}
		}

		.account_list:last-child {
			border-bottom: none;
		}
	}
</style>